package eteam.aps.systemservice.bll;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.systeminterface.model.dto.DictionaryDto;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import eteam.aps.systeminterface.model.entity.DictionaryEntity;
import eteam.aps.systeminterface.model.entity.DictypeEntity;
import eteam.aps.systeminterface.model.enums.StateEnum;
import eteam.aps.systemservice.dal.DicDal;
import eteam.aps.systemservice.model.mapper.IDicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典业务逻辑
 */
@Service
public class DicBll {
    @Autowired
    private DicDal dicDal;

    /**
     * 返回字典列表
     *
     * @return
     */
    public List<DictionaryEntity> getDicList() throws Exception {
        List<DictypeEntity> dictypeList = dicDal.getDicTypeList();
        List<DictionaryEntity> dicValueList = dicDal.getDicValueList();
        List<DictionaryEntity> retList = new ArrayList<DictionaryEntity>();
        Long idx = Long.valueOf(0);
        for (DictypeEntity item : dictypeList) {
            DictionaryEntity dicValue = new DictionaryEntity();
            dicValue.setFdicid(--idx);
            //dicValue.setFclass(item.getFcode());
            //dicValue.setFcode(item.getFcode());
            dicValue.setFname(item.getFname());
            dicValue.setChildren(dicValueList.stream().filter(p -> item.getFcode().equals(p.getFclass())).collect(Collectors.toList()));
            retList.add(dicValue);
        }
        return retList;
    }

    /**
     * 返回字典分类列表
     *
     * @return
     */
    public List<DictypeEntity> getDicTypeList() throws Exception {
        return dicDal.getDicTypeList();
    }

    /**
     * 创建字典值
     *
     * @param tokenDto
     * @param dicDto
     */
    public void createDicValue(VerifyTokenDto tokenDto, DictionaryDto dicDto) throws Exception {
        DictionaryEntity entity = IDicMapper.INSTANCE.toEntity(dicDto);
        entity.setFrecorder(tokenDto.getFuserid());
        entity.setFrecordtime(DateUtil.date());
        Integer count = dicDal.insertDicValue(entity);
        if (count < 1) {
            throw new BusinessException("新增失败");
        }
    }

    /**
     * 更新字典值
     *
     * @param tokenDto
     * @param dicDto
     */
    public void updateDicValue(VerifyTokenDto tokenDto, DictionaryDto dicDto) throws Exception {
        DictionaryEntity entity = IDicMapper.INSTANCE.toEntity(dicDto);
        entity.setFlastmodifier(tokenDto.getFuserid());
        entity.setFlastmodifytime(DateUtil.date());
        Integer count = dicDal.updateDicValue(entity);
        if (count < 1) {
            throw new BusinessException("新增失败");
        }
    }

    /**
     * 删除字典值
     *
     * @param dicId
     */
    public void deleteDicValue(Long dicId) throws Exception {
        DictionaryEntity entity = new DictionaryEntity();
        entity.setFdicid(dicId);
        entity.setState(StateEnum.Delete.getValue());
        Integer count = dicDal.updateDicValue(entity);
        //Integer count = dicDal.deleteDicValueById(dicId);
        if (count < 1) {
            throw new BusinessException("删除失败");
        }
    }

    /**
     * 获取字典值列表
     *
     * @param typeCode
     * @return
     */
    public List<DictionaryEntity> getDicByType(String typeCode) throws Exception {
        return dicDal.getDicByType(typeCode);
    }

    /**
     * 获取字典值列表
     *
     * @param typeCodes
     * @return
     */
    public List<DictionaryEntity> getDicByType(List<String> typeCodes) throws Exception {
        return dicDal.getDicByType(typeCodes);
    }

    /**
     * 返回字典分类列表
     *
     * @return
     */
    public List<DictypeEntity> getMtrParamDicTypeList() throws Exception {
        QueryWrapper<DictypeEntity> queryWrapper = new QueryWrapper<DictypeEntity>();
        queryWrapper.likeRight("FCode", "Param-");
        queryWrapper.orderByAsc("Sort");
        return dicDal.getDicTypeList(queryWrapper);
    }
}
